package two.chapter_1.c1_2;

import two.chapter_1.c1_4.BinarySearch;

import java.awt.event.KeyEvent;
import java.util.Arrays;

public class StaticSETofInts {

    private int []a;
    public StaticSETofInts(int [] keys){

        a=new int[keys.length];
        for (int i=0;i<keys.length;i++){
            a[i]=keys[i];//保护性复制
        }
        Arrays.sort(a);
    }

    public boolean contains(int key){
        return BinarySearch.rank(key,0,a.length-1,a)!=-1;
    }

    /**
     * 返回键的个数
     * @param key
     * @return
     */
    public int howMany(int key){
        int maxIndex=BinarySearch.rankMax(key,0,a.length-1,a);
        int minIndex=BinarySearch.rankMin(key,0,a.length-1,a);

        return maxIndex-minIndex+1;
    }

    /*private int rank(int key, int lo, int hi, int[] a) {

        while (lo<=hi){

            int mid=lo+(hi-lo)/2;
            int cmp=key-a[mid];
            if (cmp>0){
                lo=mid+1;
            }else if (cmp==0){
                return mid;
            }else {
                hi=mid-1;
            }

        }

        return -1;

    }*/
}
